package it.wzm.lightweightlogx;


import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

import it.wzm.lightweightlog.DBDumper;
import it.wzm.lightweightlog.DumperException;
import it.wzm.lightweightlog.Entry;


public class XDBDumper extends DBDumper {

	/**
	 * As required by serialization
	 */
	private static final long serialVersionUID = 1693145459349955269L;
	
	/**
	 * Constructor
	 * @param p
	 */
	public XDBDumper(Properties p) {
		super(p);
	}
	
	/**
	 * Overridding...
	 */
	@Override
	protected <E extends Entry> void write(E e) throws DumperException {
        try {
        	
        	XEntry x= (XEntry)e;
        	
        	PreparedStatement ps = this.getPs();
        	
            // just in case
            ps.clearParameters();
            // following the order we set parameter
            // timestamp, message, severity, classname, name, personal_key
            ps.setTimestamp(1, new java.sql.Timestamp(e.getTimestamp()));
            ps.setString(2, e.getMessage());
            ps.setString(3, e.getSeverity().toString());
            ps.setString(4, e.getClassname());
            ps.setString(5, e.getName());
            ps.setString(6, x.getPersonalKey());
            
            // ready to execute
            boolean success = ps.execute();
            if (!success) {
            	throw new DumperException("Insert fail");
            }
        } catch (SQLException ex) {
            throw new DumperException(ex);
        }		
	}

}
